package com.sun.sviewhelper;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.Nullable;

public class MyText extends androidx.appcompat.widget.AppCompatTextView {
    public MyText(Context context) {
        this(context, null);
    }

    public MyText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private Paint shadowPaint;

    private void init(Context context) {
        shadowPaint = new Paint();
        shadowPaint.setAntiAlias(true);
        shadowPaint.setStyle(Paint.Style.FILL);


    }

    private void setBackgroundCompat(int w,int h){
        Bitmap bitmap = createShadowBitmap(w,h,Color.WHITE);
        BitmapDrawable drawable = new BitmapDrawable(bitmap);

        Drawable[] drawables = new Drawable[2];
        drawables[0] = drawable;//getBg(); //new ColorDrawable(0x7f000000);
          drawables[1] = getBg();
        LayerDrawable layerDrawable = new LayerDrawable(drawables);
        int space = dp2px(10);
        layerDrawable.setLayerInset(1, space,space,space,space);

        setBackground(layerDrawable);
    }

    private Bitmap createShadowBitmap(int w,int h,int fillColor) {
        int shadowColor = Color.parseColor("#3c000000");
        int cornerRadius = dp2px(5);
        int dx = 0;
        int dy = 0;
        int width = w;
        int height = h;
        Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(output);
        int shadowRadius = dp2px(10);
        RectF shadowRect = new RectF(shadowRadius, shadowRadius, width - shadowRadius, height - shadowRadius);
        shadowPaint.setColor(fillColor);
        if(!isInEditMode()) {
            shadowPaint.setShadowLayer(shadowRadius,dx,dy,shadowColor);
        }
        canvas.drawRoundRect(shadowRect,cornerRadius,cornerRadius,shadowPaint);
        return output;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if(w > 0 && h > 0) {
            setBackgroundCompat(w,h);
        }
    }

    private int dp2px(float dp) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    private ShapeDrawable getBg() {
        int radius = 10;
        float[] outerRadii = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
        RoundRectShape roundRectShape = new RoundRectShape(outerRadii, null, null);
        ShapeDrawable shapeDrawable = new ShapeDrawable();
        shapeDrawable.setPadding(0, 0, 0, 0);
        shapeDrawable.setShape(roundRectShape);
        shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        shapeDrawable.getPaint().setColor(Color.WHITE);
        return shapeDrawable;
    }
}
